package com.ljsw.activity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ljsw.dialog.SuccessDialog;
import com.ljsw.util.DialogManager;

/**
 * @author 郑鸿博
 * @version V1.0
 * @Package com.ljsw.activity
 * @Description: TODO(修改IP地址)
 * @date 2015-12-10 上午11:14:56
 */
public class ServiceAddress extends Activity implements OnClickListener {
    private EditText nameSpace, url;
    private  EditText   nameSpacethree,urlthree;
    private Button sumbitsave;
    private String getsaveinfo;
    private String text_namespace = "namespace.txt";
    private String text_url = "url.txt";
//    boolean  text_namesprecethree=fileIsExists("text_namesprecethree.txt");
//    boolean   text_url=fileIsExists("text_url.txt");

    private String text_namespacethreee = "text_namesprecethree.txt";
    private String text_urlthree = "text_url.txt";

    private DialogManager manager;
    private SuccessDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_setweb_address);
        manager = new DialogManager(this);
        dialog = manager.getSuccessDialog();
        init();
        inputSDCardinfo(text_namespace, nameSpace);
        inputSDCardinfo(text_url, url);


        inputSDCardinfothree(text_namespacethreee, nameSpacethree);
        inputSDCardinfothree(text_urlthree, urlthree);

    }

    public void init() {
        nameSpace = (EditText) findViewById(R.id.space);
        url = (EditText) findViewById(R.id.webservice);

        nameSpacethree = (EditText) findViewById(R.id.spacethree);
        urlthree = (EditText) findViewById(R.id.webservicethree);
        sumbitsave = (Button) findViewById(R.id.changeweb);
        sumbitsave.setOnClickListener(this);
    }

    /**
     * 写入本机SD卡 文件名 控件（获取控件输入的信息）
     *
     * @param urlname
     * @param edit
     */
    public void outputSDCard(String textname, EditText edit) {
        try {
            boolean bool = fileIsExists(textname);
            File urlFile = Environment.getExternalStorageDirectory();
            File outputFile = new File(urlFile, textname);
            if (bool == false) {
                System.out.println("-----创建文件-----");
                outputFile.createNewFile();
                OutputStream os = new FileOutputStream(outputFile);
                os.write(edit.getText().toString().getBytes());
                inputSDCardinfo(textname, edit);
                System.out.println("-----写入完成-----");
            } else {
                OutputStream os = new FileOutputStream(outputFile);
                os.write(edit.getText().toString().getBytes());
                inputSDCardinfo(textname, edit);
                System.out.println("-----写入完成-----");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void outputSDCardthree(String textname, EditText edit) {
        try {
            boolean bool = fileIsExists(textname);
            File urlFile = Environment.getExternalStorageDirectory();
            File outputFile = new File(urlFile, textname);
            if (bool == false) {
                System.out.println("-----创建文件-----");
                outputFile.createNewFile();
                OutputStream os = new FileOutputStream(outputFile);
                os.write(edit.getText().toString().getBytes());
                inputSDCardinfo(textname, edit);
                System.out.println("-----写入完成-----");
            } else {
                OutputStream os = new FileOutputStream(outputFile);
                os.write(edit.getText().toString().getBytes());
                inputSDCardinfo(textname, edit);
                System.out.println("-----写入完成-----");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 本机读取保存的地址 文件名 需要赋值的控件
     *
     * @param urlname
     */
    public void inputSDCardinfothree(String textname, EditText edit) {
        try {
            boolean bool = fileIsExists(textname);
            if (bool == true) {
                File file = new File(Environment.getExternalStorageDirectory(),
                        textname);
                BufferedReader br = new BufferedReader(new FileReader(file));
                String readline = "";
                StringBuffer sb = new StringBuffer();
                while ((readline = br.readLine()) != null) {
                    sb.append(readline);
                }
                getsaveinfo = sb.toString();
                edit.setText(getsaveinfo);
                br.close();
            } else {
                outputSDCardthree(textname, edit);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     *  可以加判断  不需要写两次
     */
    public void inputSDCardinfo(String textname, EditText edit) {
        try {
            boolean bool = fileIsExists(textname);
            if (bool == true) {
                File file = new File(Environment.getExternalStorageDirectory(),
                        textname);
                BufferedReader br = new BufferedReader(new FileReader(file));
                String readline = "";
                StringBuffer sb = new StringBuffer();
                while ((readline = br.readLine()) != null) {
                    sb.append(readline);
                }
                getsaveinfo = sb.toString();
                edit.setText(getsaveinfo);
                br.close();
            } else {
                outputSDCardthree(textname, edit);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 判断文件是否存在 传入文件名
     *
     * @param textname
     * @return
     */
    public boolean fileIsExists(String textname) {
        try {
            File f = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/" + textname);
            if (!f.exists()) {
                System.out.println("-----文件不存在-----");
                return false;
            }

        } catch (Exception e) {
            return false;
        }
        System.out.println("-----文件存在-----");
        return true;
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.changeweb:
                if (!TextUtils.isEmpty(nameSpace.getText())) {
                    outputSDCard(text_namespace, nameSpace);
                }
                if (!TextUtils.isEmpty(url.getText())) {
                    outputSDCard(text_url, url);
                }

                if (!TextUtils.isEmpty(nameSpacethree.getText())) {
                    outputSDCard(text_namespacethreee, nameSpacethree);
                }
                if (!TextUtils.isEmpty(urlthree.getText())) {
                    outputSDCard(text_urlthree, urlthree);
                }



                dialog.show();
                dialog.success();
                dialog.setTitle("保存成功!");
                dialog.setSuccess(new OnClickListener() {

                    public void onClick(View arg0) {
                        inputSDCardinfo(text_namespace, nameSpace);
                        inputSDCardinfo(text_url, url);

                        inputSDCardinfo(text_urlthree, urlthree);
                        inputSDCardinfothree(text_urlthree,urlthree);
                    }
                });
                break;

            default:
                break;
        }
    }
}
